﻿using CKS.Core.Framework;
using CKS.PLC.Action.System;
using CKS.PLC.Core.Cache;
using FreeSql;
using FreeSql.Internal.Model;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription
{
    /// <summary>
    /// 容器解绑
    /// </summary>
    public class Work03Action: SaveProcessingResults
    {
        public Work03Action(DecodeValue _value) : base(_value)
        {
        }

        public override AfterSaveResult AfterSaved(SaveArg e)
        {
            
            var P = Value.Position;
            var act = conn.Select<CksPlcActions>().Where(a => a.ActionType == 1 && a.PlcId == P.PlcId && a.ActionId == P.ActionId && a.ActionKey == P.ActionKey)
                .First();
            var replyPos = Value.GetReplyPosition(act.WriteDB, act.ActionId, act.ActionKey);
            //提取关联点位的流转数据
            string TaskNo = Value.GetLinkData(1).ToString();

            

            //查询动态表记录，填充
            var fsqlTable = e.TableInfo;
            var dict = new Dictionary<string, object>();
            dict["Id"] = e.InsertIdentityId;
            dict["CheckResult"] = "2";


            
            dict["TaskNo"] = TaskNo;

            var task = conn.Select<CksPackagingTask>().Where(a => a.TaskNo == TaskNo)
                                .OrderByDescending(a => a.Id).First();
            //容器使用记录
            if (task == null)
            {
                dict["Info"] = $"任务编号不存在";
                dict["TaskNo"] = TaskNo;
                UpdateDynamicTableData(fsqlTable, dict);
                EventBus.AddLog($"任务编号{TaskNo}不存在");
                SiemensClient.SetValue(replyPos, "99");
                return new AfterSaveResult() { CanExecute = false };
            }
            dict["WaybillNo"] = task.WaybillNo;
           
            //查询任务信息进行验证
            var container = conn.Select<CksContainer>().Where(a => a.ContainerNo == task.ContainerNo&& a.ProductMode == ProductMode).First();
            if(container == null)
            {
                dict["Info"] = $"容器编号不存在";
                UpdateDynamicTableData(fsqlTable, dict);
                EventBus.AddLog($"任务号{TaskNo}关联容器{task.ContainerNo}不存在");
                SiemensClient.SetValue(replyPos, "99");
                return new AfterSaveResult() { CanExecute = false };
            }

            if (container.Status != 1)
            {
                dict["Info"] = $"容器已解绑";
                dict["TaskNo"] = container.TaskNo;
                UpdateDynamicTableData(fsqlTable, dict);
                EventBus.AddLog($"任务号{TaskNo}不存在绑定容器，或已解绑");
                SiemensClient.SetValue(replyPos, "99");
                return new AfterSaveResult() { CanExecute = false };
            }
           



            dict["TaskNo"] = task.TaskNo;
            dict["Info"] = $"解除容器绑定";
            dict["CheckResult"] = "1";
            //开始解绑
            container.Status = 0;
            task.Position = "纸箱成型位";
            task.StepNo = 3;
            task.TaskStatus = TaskStatusType.解除容器绑定;
            conn.Update<CksPackagingTask>().SetSource(task).ExecuteAffrows();
            conn.Update<CksContainer>().Set(a => a.Status, container.Status)
                .Where(a => a.Id == container.Id).ExecuteAffrows();
            UpdateDynamicTableData(fsqlTable, dict);
            EventBus.AddLog($"解除容器绑定,容器号：{task.ContainerNo}，任务号：{task.TaskNo}");

            //抓取校验成功
            return base.AfterSaved(e);
        }

        
    }
}
